package com.comum.javahomework420242025.controller;

import com.comum.javahomework420242025.mapper.DepartmentProductionMapper;
import com.comum.javahomework420242025.mapper.UserMapper;
import com.comum.javahomework420242025.pojo.*;
import com.comum.javahomework420242025.repository.DepartmentProductionRepository;
import com.github.pagehelper.PageHelper;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class DepartmentProductionController {
    @Autowired
    private DepartmentProductionRepository departmentProductionRepository;
    @Autowired
    private DepartmentProductionMapper departmentProductionMapper;
    @Autowired
    private UserMapper userMapper;
    @PostMapping("/addProductionPS")
    public Result addProductionPS(@RequestBody DepartmentProduction departmentProduction,
                                HttpServletRequest req){
        departmentProduction.setId(null);
        HttpSession session = req.getSession();
        Long userid = (Long) session.getAttribute("userid");
        String department = userMapper.joinAreaGetDepartmentByuserid(userid);
        departmentProduction.setDepartment(department);
        departmentProductionRepository.save(departmentProduction);
        return Result.success();
    }
    @PutMapping("/updateProductionPS")
    public Result updateProductionPS(
            @RequestBody DepartmentProduction departmentProduction){
        System.out.println(departmentProduction);
        departmentProductionRepository.save(departmentProduction);
        return Result.success();
    }
    @PostMapping("/getProductionPS")
    public Result<PageBean<DepartmentProductionForResult>> getProductionPS(
            @RequestBody AskList askList,HttpServletRequest req){
        int pageSize = askList.getPageSize();
        int pageNum = askList.getPageNum();
        String str = askList.getStr();
        String type = askList.getFindType();
        HttpSession session = req.getSession();
        Long userid = (Long) session.getAttribute("userid");
        String department = userMapper.joinAreaGetDepartmentByuserid(userid);
        PageBean<DepartmentProductionForResult> pb = new PageBean<>();
        //开启分页查询 ,PageHelper是个插件
        PageHelper.startPage(pageNum,pageSize);
        List<DepartmentProductionForResult> as  = departmentProductionMapper.selectByStr(str,type,department);
        com.github.pagehelper.Page<DepartmentProductionForResult> p = (com.github.pagehelper.Page<DepartmentProductionForResult>) as;
        pb.setItems(p.getResult());
        pb.setTotal(p.getTotal());
        return Result.success(pb);
    }
    @PostMapping("/inquiryProductionPS")
    public Result<PageBean<DepartmentProductionForResult>> inquiryProductionPS(
            @RequestBody AskList askList,HttpServletRequest req){
        int pageSize = askList.getPageSize();
        int pageNum = askList.getPageNum();
        String str = askList.getStr();
        HttpSession session = req.getSession();
        Long userid = (Long) session.getAttribute("userid");
        String department = userMapper.joinAreaGetDepartmentByuserid(userid);
        PageBean<DepartmentProductionForResult> pb = new PageBean<>();
        //开启分页查询 ,PageHelper是个插件
        PageHelper.startPage(pageNum,pageSize);
        List<DepartmentProductionForResult> as  = departmentProductionMapper.selectByStrOther(str,department);
        com.github.pagehelper.Page<DepartmentProductionForResult> p = (com.github.pagehelper.Page<DepartmentProductionForResult>) as;
        pb.setItems(p.getResult());
        pb.setTotal(p.getTotal());
        return Result.success(pb);
    }
    @DeleteMapping("/deleteProductionPS")
    public Result deleteProductionPS(String id){
        // TODO: 2024/12/24 最后写删除
        System.out.println(id);
        return Result.success();
    }
}
